package com.yijiu.trb;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.text.style.URLSpan;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;

import com.yijiu.action.BaseAction;
import com.yijiu.network.Request;
import com.yijiu.network.StringUtils;
import com.yijiu.resource.ActionType;
import com.yijiu.resource.C;
import com.yijiu.resource.CacheData;
import com.yijiu.response.BankResponse;
import com.yijiu.response.BaseResponse;
import com.yijiu.response.CityResponse;
import com.yijiu.utils.DialogUtils;

/**
 * 添加银行卡（输入手机号）
 * 
 * @author TangChao_Lenovo
 */
public class AddBankCardOther extends BTitleActivity
{
	private TextView bankTextView, cityTextView, nameTextView;

	/**
	 * 身份证号码输入框
	 */
	private EditText cardTextView;

	private EditText bankCardNumEditText;

	private String provinceName, cityName, bankCode;

	private Handler handler = new Handler()
	{
		@Override
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
				case 0:
					Bundle bundle = msg.getData();
					provinceName = bundle.getString("province");
					cityName = bundle.getString("city");
					cityTextView.setText(provinceName + "  " + cityName);
					break;
				case 1:
					Bundle bundle2 = msg.getData();
					String bankName = bundle2.getString("bankName");
					bankTextView.setText(bankName);
					bankCode = bundle2.getString("bankCode");
					break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_bank_card_other);
		getWindow().setFormat(PixelFormat.RGBA_8888);
		initWidget();
	}

	@Override
	protected void initWidget()
	{
		super.initWidget();
		titleView.setText("添加银行卡");
		SpannableString sp = new SpannableString("我已阅读并同意《电子商务交易转账服务条款》");
		TextView agreeTextView = (TextView) findViewById(R.id.tv_add_bank_card_other_info);
		sp.setSpan(new CheckAgreeText(""), 8, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		sp.setSpan(new ForegroundColorSpan(Color.BLACK), 8, 20, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		agreeTextView.setText(sp);
		agreeTextView.setMovementMethod(LinkMovementMethod.getInstance());
		bankTextView = (TextView) findViewById(R.id.tv_add_bank_card_other_bank);
		cityTextView = (TextView) findViewById(R.id.tv_add_bank_card_other_city);
		bankCardNumEditText = (EditText) findViewById(R.id.et_add_bank_card_other_bank_card_num);
		nameTextView = (TextView) findViewById(R.id.tv_add_bank_card_other_real_name);
		cardTextView = (EditText) findViewById(R.id.tv_add_bank_card_other_card_num);
		nameTextView.setText(CacheData.REAL_NAME);
		String idNum = CacheData.CERT_NUM;
		if (idNum.equals("null") || idNum.equals("") || idNum == null)
		{
			cardTextView.setFocusable(true);
		}
		else
		{
			cardTextView.setText(idNum);
			cardTextView.setFocusable(false);
		}
		final Button submitBankCardInfo = (Button) findViewById(R.id.next_action);
		CheckBox isAgreeCheckBox = (CheckBox) findViewById(R.id.checkBox);
		isAgreeCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
			{
				if (isChecked)
				{
					submitBankCardInfo.setEnabled(true);
					submitBankCardInfo.setBackgroundResource(R.drawable.login_background);
				}
				else
				{
					submitBankCardInfo.setEnabled(false);
					submitBankCardInfo.setBackgroundResource(R.drawable.btn_login_bg_false);
				}
			}
		});
	}

	/**
	 * 获取银行信息
	 */
	private void getBankInfo()
	{
		// 将用户输入的信息拼接成字符串上传给服务器
		String inputStr = "service=getBanksInfoService&isDeduct=IS";
		// AES加密
		encryptBase(inputStr, C.PRIVATE_KEY);
		// 封装上传的数据
		BaseAction baseAction = new BaseAction(encodedData, encriptData);
		BankResponse bankResponse = new BankResponse();
		Request r = new Request(baseAction, bankResponse, ActionType.GET_BANK_INFO);
		netManager.excute(r, this, this);
		showProgress("正在获取银行信息，请稍候！");
	}

	/**
	 * 获取省市
	 */
	private void getCityInfo()
	{
		// 将用户输入的信息拼接成字符串上传给服务器
		String inputStr = "service=appGetAllDistrictService";
		// AES加密
		encryptBase(inputStr, C.PRIVATE_KEY);
		// 封装上传的数据
		BaseAction baseAction = new BaseAction(encodedData, encriptData);
		CityResponse cityResponse = new CityResponse();
		Request r = new Request(baseAction, cityResponse, ActionType.GET_CITY_INFO);
		netManager.excute(r, this, this);
		showProgress("正在获取省市数据，请稍候！");
	}

	/**
	 * 绑定银行卡
	 */
	private void addBankCard()
	{
		String nameStr = nameTextView.getText().toString();
		String bankCardStr = bankCardNumEditText.getText().toString();
		if (StringUtils.isNull(bankCardStr))
		{
			app.showMsg("请输入银行卡号");
			return;
		}
		// 将用户输入的信息拼接成字符串上传给服务器
		String inputStr = "service=addBankCardService&bankOpenName=" + nameStr + "&bankCardNo=" + bankCardStr + "&bankCode="
				+ bankCode + "&bankProvince=" + provinceName + "&bankCity=" + cityName;
		// AES加密
		encryptBase(inputStr, C.PRIVATE_KEY);
		// 封装上传的数据
		BaseAction baseAction = new BaseAction(encodedData, encriptData, CacheData.APP_KEY);
		BaseResponse baseResponse = new BaseResponse();
		Request r = new Request(baseAction, baseResponse, ActionType.ADD_BANK_CARD);
		netManager.excute(r, this, this);
		showProgress("正在绑定银行卡，请稍候！");
	}

	/* 操作成功返回的函数 */
	@Override
	public void getResult(Request r)
	{
		super.getResult(r);
		switch (r.getType())
		{
			case ActionType.GET_BANK_INFO:
				BankResponse bankResponse = (BankResponse) r.getResponse();
				DialogUtils.showBankDialog(this, bankResponse.bankList, handler);
				break;
			case ActionType.GET_CITY_INFO:
				CityResponse cityResponse = (CityResponse) r.getResponse();
				DialogUtils.showCityDialog(this, cityResponse.cityListMap, cityResponse.provinceList, handler);
				break;
			case ActionType.ADD_BANK_CARD:
				app.showMsg("绑定银行卡成功！");
				Intent intent = getIntent();
				setResult(RESULT_OK, intent);
				finish();
				break;
		}
	}

	/* 操作失败返回的函数 */
	@Override
	public void notifyError(int type, int errorCode, String errorMsg)
	{
		super.notifyError(type, errorCode, errorMsg);
		switch (type)
		{
			case ActionType.GET_BANK_INFO:
			case ActionType.GET_CITY_INFO:
			case ActionType.ADD_BANK_CARD:
				app.showMsg(errorMsg);
				break;
		}
	}

	/* 重复提交返回的函数 */
	@Override
	public void rePost(Request r)
	{
		super.rePost(r);
	}

	public void addBankCardOtherClick(View v)
	{
		int id = v.getId();
		switch (id)
		{
			case R.id.tv_add_bank_card_other_city:
				getCityInfo();
				break;
			case R.id.llayout_add_bank_card_other_bank:
				getBankInfo();
				break;
			case R.id.next_action:
				addBankCard();
				break;
			default:
				break;
		}
	}

	class CheckAgreeText extends URLSpan
	{
		public CheckAgreeText(String src)
		{
			super(src);
		}

		@Override
		public void onClick(View widget)
		{
			DialogUtils.showAgreeTxtDialog(AddBankCardOther.this, R.string.txt1);
		}
	}
}
